import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Layout',
    redirect: "/home",
    component: () => import('@/layout/Layout.vue'),
    children: [
      {
        path: 'home',
        name: 'Main',
        component: () => import('@/views/Main.vue'),
      },
      {
        path: 'mes',
        name: 'Mes',
        component: () => import('@/views/Mes.vue'),
      },
      {
        path: 'see',
        name: 'See',
        component: () => import('@/views/See.vue'),
      },
      {
        path: 'person',
        name: 'Person',
        component: () => import('@/views/Person.vue'),
      },
      {
        path: 'publish',
        name: 'Publish',
        component: () => import('@/views/Publish.vue'),
      },
      {
        path: 'buy',
        name: 'Buy',
        component: () => import('@/views/Buy.vue'),
      },
      {
        path: 'goodsMess',
        name: 'goodsMess',
        component: () => import('@/views/goodsMess.vue'),
      },
      {
        path: 'im',
        name: 'Im',
        component: () => import('@/views/Im.vue'),
      },
    ],
    meta: {
      requireAuth: true
    },
  },
  {
    path:'/login',
    name:'login',
    component:() =>import('../views/Login.vue')
  },
  {
    path: '/register',
    name:'register',
    component:() =>import('../views/Register.vue')
  }
]

const router = createRouter({
  history: createWebHistory("/"),
  routes: routes
})

export default router

//还是在router.js中 我们这里就放在 export default router 的后面
//登录拦截
router.beforeEach((to, from, next) => {
    if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
        //如果需要就执行下面的代码
        
        var num=sessionStorage.getItem("token");
        // 通过sessionStorage 获取当前的isLogin的值 将我们保存的isLogin的值赋给num,num是顺便取的名称，可以换
        //我们在登录界面，我们使用请求，请求成功后，我们就使用sessionStorage为‘isLogin’保存一个值  1，如果请求失败，就不保存‘isLogin’的值
        //如果请求成功，num的值就是1，请求失败就是null，所以下面进行判断
        if (num != null) {  
            //如果登录了，就跳转到'/'路径
            next();
        }
		else {
            next({
                path: '/login',//返回登录界面
                // query: {redirect: to.fullPath}  
            })
        }
    }
    else {
        //如果不需要登录权限就直接跳转到该路由
        next();
    }
})

// import Vue from 'vue'
// import Router from 'vue-router'

// Vue.use(Router)

// /* Layout */
// import Layout from '@/layout'

// /**
//  * Note: sub-menu only appear when route children.length >= 1
//  * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
//  *
//  * hidden: true                   if set true, item will not show in the sidebar(default is false)
//  * alwaysShow: true               if set true, will always show the root menu
//  *                                if not set alwaysShow, when item has more than one children route,
//  *                                it will becomes nested mode, otherwise not show the root menu
//  * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
//  * name:'router-name'             the name is used by <keep-alive> (must set!!!)
//  * meta : {
//     roles: ['admin','editor']    control the page roles (you can set multiple roles)
//     title: 'title'               the name show in sidebar and breadcrumb (recommend set)
//     icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
//     breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
//     activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
//   }
//  */

// /**
//  * constantRoutes
//  * a base page that does not have permission requirements
//  * all roles can be accessed
//  */
// export const constantRoutes = [
//   {
//     path: '/',
//     name: 'Layout',
//     redirect: "/home",
//     component: () => import('@/layout/Layout.vue'),
//     children: [
//       {
//         path: 'home',
//         name: 'Main',
//         component: () => import('@/views/Main.vue'),
//       },
//       {
//         path: 'mes',
//         name: 'Mes',
//         component: () => import('@/views/Mes.vue'),
//       },
//       {
//         path: 'see',
//         name: 'See',
//         component: () => import('@/views/See.vue'),
//       },
//       {
//         path: 'person',
//         name: 'Person',
//         component: () => import('@/views/Person.vue'),
//       },
//       {
//         path: 'publish',
//         name: 'Publish',
//         component: () => import('@/views/Publish.vue'),
//       },
//       {
//         path: 'buy',
//         name: 'Buy',
//         component: () => import('@/views/Buy.vue'),
//       },
//     ],
//     meta: {
//       requireAuth: true
//     },
//   },
//   {
//     path:'/login',
//     name:'login',
//     component:() =>import('../views/Login.vue')
//   },
//   {
//     path: '/register',
//     name:'register',
//     component:() =>import('../views/Register.vue')
//   },

//   {
//     path: '/404',
//     component: () => import('@/views/404'),
//     hidden: true
//   },

//   // 404 page must be placed at the end !!!
//   { path: '*', redirect: '/404', hidden: true }
// ]

// const createRouter = () => new Router({
//   // mode: 'history', // require service support
//   scrollBehavior: () => ({ y: 0 }),
//   routes: constantRoutes
// })

// const router = createRouter()

// // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// export function resetRouter() {
//   const newRouter = createRouter()
//   router.matcher = newRouter.matcher // reset router
// }

// export default router

// //还是在router.js中 我们这里就放在 export default router 的后面
// //登录拦截
// router.beforeEach((to, from, next) => {
//     if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
//         //如果需要就执行下面的代码
        
//         var num=sessionStorage.getItem("isLogin");
//         // 通过sessionStorage 获取当前的isLogin的值 将我们保存的isLogin的值赋给num,num是顺便取的名称，可以换
//         //我们在登录界面，我们使用请求，请求成功后，我们就使用sessionStorage为‘isLogin’保存一个值  1，如果请求失败，就不保存‘isLogin’的值
//         //如果请求成功，num的值就是1，请求失败就是null，所以下面进行判断
//         if (num==1) {  
//             //如果登录了，就跳转到'/index'路径
//             next({path:'/index'});
//         }
// 		else {
//             next({
//                 path: '/login',//返回登录界面
//                 // query: {redirect: to.fullPath}  
//             })
//         }
//     }
//     else {
//         //如果不需要登录权限就直接跳转到该路由
//         next();
//     }
// })

